package QAInfo::TT::ReportFilter;

# 
sub format_os {
    my $text = shift;
    $text =~ s/Microsoft Windows/Win/g;
    $text =~ s/Service\sPack/SP/gi;
    $text =~ s/\d\.\d\.\d+//gi; # Cut version
    return $text;
}


sub _format_statistics_data {
    my $text = shift;
    # Final results:
    # Elapsed: 03 day(s), 13:19:13
    # Passed: 550 test(s) (99%)
    # Failed: 4 test(s) (0%)
    return unless defined $text;

    my ( $passed, $failed, $elapsed, $total) = (0, 0, '',0);

    $passed  = $1 if ($text =~ /Passed:\s+?(\d+)\s+test/s);
    $failed  = $1 if ( $text =~ /Failed:\s+?(\d+)\s+test/s );
    $elapsed = $1 if ( $text =~ /Elapsed:\s+?(.+)\n/s );
    $total   = $passed + $failed;

    my $result = {
                   elapsed => $elapsed,
                   passed  => $passed,
                   failed  => $failed,
                   total   => $total
                 };

    return $result;
}

sub format_statistics {
    my $text = shift;
    my $st = _format_statistics_data $text;

    my $passed  = $st->{passed};
    my $failed  = $st->{failed};
    my $total   = $st->{total} || 1;
    my $elapsed = $st->{elapsed};

    my $stat = qq[<span class="stat-results">
                       <span class="stat-pass">P:  $passed </span>
                       <span class="stat-fail">F:  $failed </span>
                       <span class="stat-total">T: $total  </span>
                  </span>];

    my $pass_percent = $passed * 100 / $total;
    my $fail_percent = 100 - $pass_percent;
    my $p_width = (int $pass_percent) * 2;
    my $f_width = (int $fail_percent) * 2; 

    my $progress = qq[<img src="/static/images/green.png" width="$p_width" height="16">].
                   qq[<img src="/static/images/red.png" width="$f_width" height="16">];
    $text = "$progress<br>$stat";

    return $text;
}


sub format_verdict {
    my $text = shift;
    my $css_class = 'verdict-unknown';

    if ( $text =~ /NEW|KNOWN/i ) {
        $css_class = 'verdict-known';
    }
    elsif ($text =~ /FAIL|CRASH/i) {
        $css_class = 'verdict-failed';
    }
    elsif ($text =~ /PASS/i) {
        $css_class = 'verdict-passed';
    }

    $text =qq[<span class="verdict"><span class="$css_class">$text<span></span>];
    return $text;
}

1;